<template>
  <div class="form-div">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="User Name" prop="user_name">
        <el-input v-model="ruleForm.user_name"></el-input>
      </el-form-item>
      <el-form-item label="Password" prop="pass">
        <el-input
          type="password"
          v-model="ruleForm.pass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="Confirm Password" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">
          Submit
        </el-button>
        <el-button @click="resetForm('ruleForm')">Reset</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { regist } from "../api/user"

  export default {
    name: "Register",
    data() {
      // var checkUsername = (rule, value, callback) => {
      //   if (!value) {
      //     return callback(new Error("用户名不能为空"))
      //   }
      // }
      var validatePass = (rule, value, callback) => {
        if (value === "") {
          callback(new Error("Please enter password"))
        } else {
          if (this.ruleForm.checkPass !== "") {
            this.$refs.ruleForm.validateField("checkPass")
          }
          callback()
        }
      }
      var validatePass2 = (rule, value, callback) => {
        if (value === "") {
          callback(new Error("Please enter password again"))
        } else if (value !== this.ruleForm.pass) {
          callback(new Error("Two password not match"))
        } else {
          callback()
        }
      }
      return {
        ruleForm: {
          pass: "",
          checkPass: "",
          user_name: "",
        },
        rules: {
          pass: [{ validator: validatePass, trigger: "blur" }],
          checkPass: [{ validator: validatePass2, trigger: "blur" }],
          // user_name: [{ validator: checkUsername, trigger: "blur" }],
        },
      }
    },
    mounted() {},
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            regist(this.ruleForm.user_name, this.ruleForm.pass).then((res) => {
              console.log("res: ", res)
              if (res.data.errors != undefined) {
                res.data.errors.forEach((element) => {
                  this.$message.error(element.message)
                })
              } else {
                this.$message.success("create user success")
                this.$router.push({ path: "/login" })
              }
            })
          } else {
            this.$message.error("submit fail!")
            return false
          }
        })
      },
      resetForm(formName) {
        this.$refs[formName].resetFields()
      },
    },
  }
</script>

<style>
  .form-div {
    padding-left: 25em;
    padding-right: 25em;
    padding-top: 4em;
  }
</style>
